package com.treeman.controller.user;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.treeman.user.User;
import com.treeman.user.service.UserService;

@Controller
public class FriendsController {

	@Autowired
	private UserService userService;

	@RequestMapping(value = "/friends", method = RequestMethod.GET)
	public ModelAndView showFriends() {
		String username = SecurityContextHolder.getContext()
				.getAuthentication().getName();
		Set<User> friends = userService.getUserFriends(username);

		return new ModelAndView("user/friends/friends", "friends", friends);
	}

	@RequestMapping(value = "/friends/search", method = RequestMethod.GET)
	public ModelAndView searchFriends(@RequestParam String firstname,
			@RequestParam String lastname, @RequestParam String username) {

		List<User> result = userService
				.findUsers(firstname, lastname, username);
		return new ModelAndView("user/friends/search-result", "searchResult",
				result);
	}
}
